/* 
 *  Copyright 2012 CodeMagi, Inc.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.codemagi.servlets.upload.model;

import java.io.Serializable;
import java.util.Date;

/**
 * Models an upload.
 *
 * @version 1.0
 * @author August Detlefsen for CodeMagi, Inc.
 */
public class Upload extends com.codemagi.servlets.model.Node implements Serializable {

    //memebers
    private FileType fileType;
    private String link;
    private String description;
    private Integer sortOrder;
    private Boolean isOnline     = new Boolean(true);
    private Boolean isPublic     = new Boolean(true);
    private Boolean isWatermark  = new Boolean(true);
    private Date fileDate        = new Date();
    private Long size;


    //GETTERS AND SETTERS --------------------------------------------

    public void setFileType(FileType newValue) {
	fileType = newValue;
    }

    public FileType getFileType() {
	return fileType;
    }

    public void setLink(String newValue) {
        link = newValue;
    }

    public String getLink() {
        return link;
    }

    public void setDescription(String newValue) {
        description = newValue;
    }

    public String getDescription() {
        return description;
    }

    public void setOnline(Boolean newValue) {
	//no nulls allowed
	if (newValue == null) return;

        isOnline = newValue;
    }

    public Boolean getOnline() {
        return isOnline;
    }


    public void setPublic(Boolean newValue) {
	//no nulls allowed
	if (newValue == null) return;

        isPublic = newValue;
    }

    public Boolean getPublic() {
        return isPublic;
    }


    public void setFileDate(Date newValue) {
	//no nulls allowed
	if (newValue == null) return;

        fileDate = newValue;
    }

    public Date getFileDate() {
        return fileDate;
    }

    public void setSize(Long newValue) {
        size = newValue;
    }

    public Long getSize() {
        return size;
    }
    
    public void setIsWatermark(Boolean newValue) {
        if (newValue != null) isWatermark = newValue;
    }

    public Boolean getIsWatermark() {
        return isWatermark;
    }


    //SPECIAL GETTERS AND SETTERS ----------------------------------------

    public boolean isWatermark() {
        return isWatermark.booleanValue();
    }

    public boolean isImage() {
	if (fileType == null) return false;

	return fileType.isImage();
    }

    public boolean isPublic() {
	return isPublic.booleanValue();
    }

    public String getFileName() {
        if (getLink() != null) {
            return getLink().substring(getLink().lastIndexOf("/") + 1);
        }
        return null;
    }

    public String getExtension() {
	if (fileType == null) return "";

	return fileType.getExtension();
    }

    //OBJECT METHODS OVERRIDDEN TO HAVE USEFUL RESULTS -------------------

    public String toString() {
	return super.toString() + 
	    " [ID: " + getId() + 
	    " NAME: " + getName() + 
	    " TYPE: " + fileType + 
	    " LINK: " + link + 
	    " DESC: " + description + 
	    " ONLINE: " + isOnline + 
	    " PUBLIC: " + isPublic + 
	    " FILE DATE: " + fileDate + 
	    " SIZE: " + size + 
	    "]";
    }

}
